# Test PHDRS in a linker script.
# By Ian Lance Taylor, Cygnus Support.
#   Copyright (C) 1999-2015 Free Software Foundation, Inc.
#
# This file is part of the GNU Binutils.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
# MA 02110-1301, USA.

# PHDRS is only meaningful for ELF.
if ![is_elf_format] {
    return
}

# This is a very simplistic test.

set testname "PHDRS"

set ldopt ""
if { [istarget spu*-*-*] } {
    set ldopt "--local-store 0:0"
}

if ![ld_assemble $as $srcdir/$subdir/phdrs.s tmpdir/phdrs.o] {
    unresolved $testname
    return
}

set phdrs_regexp \
".*Program Header:.*PHDR *off *0x00*34 *vaddr *0x00*800034 *paddr *0x00*800034.*filesz *0x0\[0-9a-f\]* *memsz *0x0\[0-9a-f\]* flags r--.*LOAD *off *0x00* *vaddr *0x00*800000 *paddr *0x00*800000.*filesz *0x00*\[0-9a-f\]* *memsz *0x0\[0-9a-f\]* *flags r-x.*LOAD *off *0x0\[0-9a-f\]* *vaddr *0x00*80*\[0-9a-f\]* *paddr *0x00*80*\[0-9a-f\]*.*filesz *0x0\[0-9a-f\]* *memsz *0x0\[0-9a-f\]* *flags *rw-.*"

# On a 64 bit ELF format, we need different numbers.
if [is_elf64 tmpdir/phdrs.o] {
    set phdrs_regexp \
".*Program Header:.*PHDR *off *0x00*40 *vaddr *0x00*800040 *paddr *0x00*800040.*filesz *0x0\[0-9a-f\]* *memsz *0x0\[0-9a-f\]* flags r--.*LOAD *off *0x00* *vaddr *0x00*800000 *paddr *0x00*800000.*filesz *0x00*\[0-9a-f\]* *memsz *0x0\[0-9a-f\]* *flags r-x.*LOAD *off *0x0\[0-9a-f\]* *vaddr *0x00*80*\[0-9a-f\]* *paddr *0x00*80*\[0-9a-f\]*.*filesz *0x0\[0-9a-f\]* *memsz *0x0\[0-9a-f\]* *flags *rw-.*"
}

set ldopt "$ldopt -T $srcdir/$subdir/phdrs.t tmpdir/phdrs.o"
if ![ld_simple_link $ld tmpdir/phdrs $ldopt] {
    fail $testname
} else {
    if {![is_remote host] && [which $objdump] == 0} {
	unresolved $testname
	return
    }
    set exec_output [run_host_cmd "$objdump" "--private-headers tmpdir/phdrs"]
    set exec_output [prune_warnings $exec_output]
    verbose -log $exec_output

    # The RX port sets the p_paddr of loadable segments to 0 in order
    # to be compatible with Renesas tools.  When an RX executable is
    # loaded into a BFD based tool the code tries to reconstruct the
    # correct vaddr and paddr values.  This is not always possible
    # however and this test is one example of where the reconstruction
    # fails.
    setup_xfail rx-*-*
    
    if [regexp $phdrs_regexp $exec_output] {
	pass $testname
    } else {
	fail $testname
    }
}
